package com.ysq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ysq.common.BaseContext;
import com.ysq.entity.AddressBook;
import com.ysq.mapper.AddressBookMapper;
import com.ysq.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: AddressBookServiceImpl
 * Package: com.ysq.service.impl
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/6 - 9:22
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper mapper;

    @Override
    public boolean save(AddressBook addressBook) {
        //拿到当前用户ID
        Long currentId = BaseContext.getCurrentId();
        //将当前用户ID封装到addressbook的useId属性
        addressBook.setUserId(currentId);
        //将地址信息保存到数据库
        int row = mapper.insert(addressBook);
        return row > 0;
    }

    @Override
    public List<AddressBook> selectList() {
        //拿到当前用户ID
        Long currentId = BaseContext.getCurrentId();
        //构造条件构造器
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        //插入条件
        lqw.eq(AddressBook::getUserId, currentId);//where user_id = currentId;
        lqw.orderByDesc(AddressBook::getUpdateTime);//根据更新时间倒序排列
        //执行查询 select * from address_book where user_id = currentId order by update_time desc;
        List<AddressBook> addressBooks = mapper.selectList(lqw);
        return addressBooks;
    }

    @Override
    @Transactional
    public AddressBook setDefault(AddressBook addressBook) {
        //这个思路就要好好捋一捋:
        //把这个地址设为默认地址, 首先要做的就是把当前用户关联的所有地址的is_default字段设为0
        //1. 拿到当前用户ID
        Long currentId = BaseContext.getCurrentId();
        //2. 构造条件构造器 ==> 此时是更新条件构造器 而不是查询条件构造器
        //因为我们最终执行的语句是 update address_book set is_default = 0 where user_id = ?;
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        //3. 设置条件
        luw.eq(AddressBook::getUserId, currentId);//where user_id = ?;
        luw.set(AddressBook::getIsDefault, 0);//set is_default = 0
        //4. 更新address_book表
        int row = mapper.update(null, luw);

        //上面的都做完了, 也就代表当前用户关联的地址is_default字段都是0
        //我们再把这个地址的is_default字段设为1
        addressBook.setIsDefault(1);
        mapper.updateById(addressBook);//update address_book set is_default = 1 where id = ?
        return addressBook;
    }

    @Override
    public AddressBook selectAddress(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public boolean update(AddressBook addressBook) {
        int row = mapper.updateById(addressBook);
        return row > 0;
    }

    @Override
    public boolean delete(Long id) {
        int row = mapper.deleteById(id);
        return row > 0;
    }

    @Override
    public AddressBook getDefault() {
        //获取当前用户ID
        Long currentId = BaseContext.getCurrentId();
        //我们最终要执行的SQL语句如下:
        //select * from address_book where user_id = ? and is_default = 1;
        //1. 构建条件构造器
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        //2. 插入条件
        lqw.eq(AddressBook::getUserId, currentId);//where user_id = ?
        lqw.eq(AddressBook::getIsDefault, 1);//where is_default = 1
        //3. 执行查询
        AddressBook addressBook = mapper.selectOne(lqw);
        return addressBook;
    }
}
